import PageModel from "../../models/Page";
import ImageModel from "../../models/Image";

import CartModel from "../../models/Cart";
import Crypt from "../../utils/Crypt";
import CartTotal from "../../utils/CartTotal";

const cartModel = new CartModel()

const pageModel = new PageModel()
const imageModel = new ImageModel()

Page({
  data: {},
  onLoad: function (options) {

    console.log(options)

    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success: () => {
        // console.log('ok')
        // 可以把分享记录保存到数据库
      }
    })

    // uni
    pageModel.getHome().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      this.setData({ ...data })
      // console.log(this.data.nav)
    }).catch(err => {

    })

    // image
    imageModel.getRemoteImage().then(data => {
      this.setData({
        image: data
      })
    }).catch(err => {

    })

    // referrer
    if (options.scene && (options.scene > 0 || options.scene.length > 0)) {

      console.log(options.scene)

      let id = 0
      let product_id = 0
      if (options.scene.indexOf('-') > -1) {
        const temp = options.scene.split('-')
        if (temp && temp.length === 2) {
          id = temp[1]
          product_id = temp[0]
        }
      } else {
        id = options.scene
      }
      pageModel.setReferrer(id)

      // console.log('推荐人ID: ', id)

      if (product_id > 0) {
        wx.navigateTo({
          url: '/sub_product/pages/detail/index?id=' + product_id
        })
      }
    }

    // 购物车数量 (这个不用登录)
    cartModel.listTotal().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      CartTotal.total(data)
    })
  }
});
